package com.mornd.gulimall.order.receiver;

import com.mornd.gulimall.order.entity.Order;
import com.mornd.gulimall.order.service.IOrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author: mornd
 * @dateTime: 2023/8/27 - 12:25
 * 关闭订单
 */

@Slf4j
@Component
@RabbitListener(queues = "order.release.order.queue")
public class OrderCloseReceiver {
    @Resource
    private IOrderService orderService;

    @RabbitHandler
    public void handle(Message message, Order order, Channel channel) throws IOException {
        // todo 手动调用支付宝收单，通知用户支付已过期，不能支付了
        try {
            log.debug("正在关闭订单，{}", order);
            orderService.closeOrder(order);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error(e.getMessage());
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
